{% extends 'base/base.html' %}
{% load tags %}

{% block css %}
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block title %}ITAdmin | 终端--添加用户{% endblock %}


{% block body %}
<form class="form-horizontal" method="post">
    {% csrf_token %}
    <div class="hr-line-dashed"></div>
    <div class="form-group"><label class="col-sm-2 control-label">选择用户和主机</label>
        <div class="col-sm-10">
            <select class="form-control m-b" name="account" id="user">
                {% for user in users %}
                    {% judge_userid user selected_user %}
                {% endfor %}
            </select>

            <div class="col-lg-4 m-l-n">
                <select class="form-control" multiple="multiple" size="10" id="server" name="server">
                        {% for server in servers %}
                            {% if not selecetd_servers %}
                                <option value="{{ server.id }}">{{ server.name }}</option>
                            {% else %}
                                {% judge_server server selecetd_servers %}
                            {% endif %}
                        {% endfor %}
                </select>
            </div>
        </div>
    </div>
    <div class="hr-line-dashed"></div>
    <div class="col-sm-4 col-sm-offset-5">
        <button class="btn btn-outline btn-warning dim" type="button" id="cancel"><i class="fa fa-warning"></i>取消</button>
        <button class="btn btn-primary dim" type="button" id="add"><i class="fa fa-check"></i>保存</button>
    </div>
</form>
{% endblock %}

{% block base_script %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
{% endblock %}

{% block script %}
<script type="text/javascript">
    $(function () {
        $("#add").click(function () {
            var servers = new Array();
            for (var i = 0; i < $("#server option:selected").length; i++) {
                var o = $("#server option:selected")[i];
                servers.unshift($(o).attr("value"));
            }
            if (servers.length == 0) {
                swal({title:"错误!", text:"请选择主机", type:"error"});
                return;
            }
            var user = $('#user').val();
            $.post("{% url 'addterminaluser' %}", {user: user, servers: JSON.stringify(servers)},function (data) {
                if (data.status == 1) {
                    swal({title:"成功!", text:"添加终端用户成功", type:"success"},function () {
                        location.href = "{% url 'terminals' %}";
                    });

                } else {
                    swal({title:"错误!", text:data.msg, type:"error"}, function () {
                        location.href = "{% url 'addterminaluser' %}";
                    });
                }
            }, 'json');
        })
        $('#cancel').click(function () {
            location.href = "{% url 'addterminaluser' %}";
        })
    });
</script>
{% endblock %}